package com.yf1706.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.resource.beans.container.internal.NoSuchBeanException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yf1706.bean.Dep;
import com.yf1706.bean.Emp;
import com.yf1706.bean.EmpPositionChangeRecord;
import com.yf1706.bean.PostSetting;
import com.yf1706.bean.RankSetting;
import com.yf1706.bean.User;
import com.yf1706.service.ApproveService;
import com.yf1706.service.DepService;
import com.yf1706.service.EmpService;
import com.yf1706.service.SettingService;
import com.yf1706.service.UserService;
import com.yf1706.vo.Information;
import com.yf1706.vo.UserEmpDepVO;

@Controller
@RequestMapping("personnel")
public class PersonnelController {
	@Autowired
	private EmpService es;
	@Autowired
	private ApproveService as;
	@Autowired
	private DepService ds;
	@Autowired
	private SettingService ss;
	@Autowired
	private UserService us;
	@RequestMapping("personnelHome")
	public String personnel() {
		System.out.println("进入personnelHome");
		return "zxy/personnel_home";
	}
	
	@RequestMapping("empList")
	public String empList() {
		System.out.println("进入empList");
		return "zxy/emp_list";
	}
	
	
	@RequestMapping("userGuide")
	public String userGuider() {
		System.out.println("进入userGuide");
		return "zxy/user_guide";
	}
	
	//员工职务变动页面
	@RequestMapping("positionChange")
	public String positionChange() {
		System.out.println("进入position_change");
		return "zxy/position_change";
	}
	
	//员工职务变动页面
	@RequestMapping("empHire")
	public String empHire() {
		System.out.println("进入empHire");
		return "zxy/emp_hire";
	}
	
	//增加员工职务变动记录
	@RequestMapping("addPositionChange")
	@ResponseBody
	public Information addPositionChange(HttpServletRequest request) {
		System.out.println("进入addPositionChange");
			EmpPositionChangeRecord pcr=new EmpPositionChangeRecord();
			int empId=0;
			if(!request.getParameter("empId").equals("")) {
				empId=Integer.parseInt(request.getParameter("empId"));
				Emp emp=es.queryById(empId);
				pcr.setEmpId(empId);
				pcr.setEmpName(emp.getEmpName());
				pcr.setPost(emp.getPostSetting().getPostName());
				pcr.setDep(emp.getDep().getDepName());
				
				pcr.setOriginalDep(emp.getDep().getDepName());
				pcr.setOriginalPost(emp.getPostSetting().getPostName());
				pcr.setOriginalRank(emp.getRankSetting().getRankName());
			}
			int newDep=0;
			if(!request.getParameter("newDep").equals("")) {
				newDep=Integer.parseInt(request.getParameter("newDep"));
				Dep dep=ds.findByDepId(newDep);
				pcr.setNewDep(dep.getDepName());
			}
			int newPost=0;
			if(!request.getParameter("newPost").equals("")) {
				newPost=Integer.parseInt(request.getParameter("newPost"));
				PostSetting post=ss.findPostSettingByPostId(newPost);
				pcr.setNewPost(post.getPostName());
			}
			int newRank=0;
			if(!request.getParameter("newRank").equals("")) {
				newRank=Integer.parseInt(request.getParameter("newRank"));
				RankSetting rank=ss.findRanktSettingByRankId(newRank);
				pcr.setNewRank(rank.getRankName());
			}
			int approverId=0;
			if(!request.getParameter("approver").equals("")) {
				approverId=Integer.parseInt(request.getParameter("approver"));
				UserEmpDepVO ued=us.fundUserBasicByUserId(approverId);
				pcr.setApproverId(approverId);
				pcr.setApprover(ued.getUserName());
				pcr.setApproveDepId(ued.getDepId());
				pcr.setApproveDep(ued.getDepName());
				pcr.setApproveStatus((byte)1);
			}
			int applyId=((User) request.getSession().getAttribute("user")).getUserId();
			UserEmpDepVO ued=us.fundUserBasicByUserId(applyId);
			pcr.setApplyEmpId(ued.getUserId());
			pcr.setApplyEmpName(ued.getUserName());
			pcr.setApplyDate(new Date());
			
			String approverDep=request.getParameter("approverDep");
			String docNum=request.getParameter("docNum");
			String remark=request.getParameter("remark");
			
			pcr.setDocNum(docNum);
			pcr.setRemark(remark);
			System.out.println(pcr);
			as.addPositionChangeRecord(pcr);
			return Information.SUCCESS;
	}
	
	//用户角色权限设置
	@RequestMapping("roleSetting")
	public String roleSetting() {
		System.out.println("进入roleSetting");
		return "zxy/role_list";
	}
}
